const experiment = require('../models/experiment')
/**
 * 
 * 创建实验
 * 
 */
var create = async (ctx,next)=>{
    let document = await experiment.findOne({title:ctx.request.body.title});
    if(document){
        return ctx.response.body = {stauts:'error',msg:'实验已存在'};
    }
    var title = ctx.request.body.title;
    var purpose = ctx.request.body.purpose;
    var content = ctx.request.body.content;
    var endDate = ctx.request.body.endDate;
    var ex_class = ctx.request.body.class;
    var step = ctx.request.body.step;
    var accessory = ctx.request.body.accessory;
    var remark = ctx.request.body.remark;

    let experimentEntity = new experiment({
        title:title,
        purpose:purpose,
        content:content,
        endDate:endDate,
        class:ex_class,
        step:step,
        accessory:accessory,
        remark:remark,
    })
    experimentEntity.save(function(err,doc){
        if(err){
            console.log(err);
        }else{
            console.log(doc);
        }
    })
    return ctx.response.body = {stauts:'success',msg:'成功',experimentEntity};
}
/**
 * 
 * 查找指定课程的全部实验
 * 
 * 
 */

var getAllexperiment = async (ctx,next)=>{
    var allEx = await experiment.find({class:ctx.request.body.ex_class},function(err,docs){
         if(!err){

        }else{
            console.log(err);
        }
    })
    return ctx.response.body = {stauts:'success',msg:'成功',allEx};
}

/**
 * 
 * 删除实验
 * 
 */
var delExperiment = async (ctx,next)=>{
    try{
        await experiment.remove({title:ctx.request.body.title})
        // let exID = delEx._id;
        // experiment.findByIdAndRemove(exID)
        return ctx.response.body = {stauts:'success',msg:'成功'};
    }catch(err){
        console.log(err)
    }
}

/**
 * 获取指定实验
 * 
 */
var getExperiment = async (ctx,next)=>{
    try{
        var getEx = await experiment.findOne({title:ctx.request.body.title})
        return ctx.response.body = {stauts:'success',msg:'成功',getEx};
    }catch(err){
        console.log(err)
    }
}
module.exports = {
    'POST /experiment/create': create,
    'POST /experiment/getAllexperiment': getAllexperiment,
    'POST /experiment/delExperiment': delExperiment,
    'POST /experiment/getExperiment': getExperiment,
}